package message;

import lombok.extern.java.Log;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.util.StringUtils;

import javax.jms.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Objects;
import java.util.StringTokenizer;

/**
 * Created with Intel IDEA.
 * 借方
 *
 * @Auther: gjt
 * @Date: 2024/11/3 15:07
 */

@Log
public class QBorrower {
    private static String USERNAME = ActiveMQConnection.DEFAULT_USER;
    private static String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    private static String BROKER = ActiveMQConnection.DEFAULT_BROKER_URL;
    private QueueConnection qConnect = null;
    private QueueSession qSession = null;
    private Queue responseQ = null;
    private Queue requestQ = null;

    public QBorrower(String queuecf,String requestQueue, String responseQueue) throws Exception {
        QueueConnectionFactory conFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER);
        qConnect = conFactory.createQueueConnection();

        // 创建会话
        qSession = qConnect.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

        // 创建请求队列|响应队列
        requestQ = qSession.createQueue(requestQueue);
        responseQ = qSession.createQueue(responseQueue);

        // 创建完成，启动链接
        qConnect.start();
    }

    private void sendLoanRequest(double salary, double loanAmt){
        try {
            //创建消息
            MapMessage message = qSession.createMapMessage();
            message.setDouble("salary", salary);
            message.setDouble("loanAmt", loanAmt);
            // 请求队列必须响应
            message.setJMSReplyTo(responseQ);
            message.setJMSDeliveryMode(1);

            // 创建发送者并发送消息
            QueueSender qSessionSender = qSession.createSender(requestQ);
            qSessionSender.send(message);

            // 等待查看贷款申请被接收或者拒绝
            String filter = "JMSCorrelationID = '" + message.getJMSMessageID() + "'";
            QueueReceiver qReceiver = qSession.createReceiver(responseQ, filter);
            TextMessage tmsg = (TextMessage) qReceiver.receive(30000);

            if(Objects.isNull(tmsg)){
                System.out.println("QLender 没有响应");
            }else {
                System.out.println("QLender 的响应是:" + tmsg.getText());
            }
        }catch (JMSException e){
            log.warning(e.getMessage());
        }
    }

    private void exit(){
        try {
            qConnect.close();
        }catch (JMSException e){
            log.warning(e.getMessage());
        }
    }

    public static void main(String[] args) throws Exception {
        QBorrower qBorrower = new QBorrower("QCF", "QBorrower", "QLender");

        try{
            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

            while(true){
                System.out.println("> ");
                String loanRequest = stdin.readLine();
                if(StringUtils.isEmpty(loanRequest)){
                    qBorrower.exit();
                }

                // 解析交易说明
                StringTokenizer st = new StringTokenizer(loanRequest, ",");
                double salary = Double.parseDouble(st.nextToken().trim());
                double loanAmt = Double.parseDouble(st.nextToken().trim());

                qBorrower.sendLoanRequest(salary, loanAmt);
            }
        }catch (Exception e){
            log.warning(e.getMessage());
        }
    }
}
